package main

import (
	"fmt"
	"time"
	"github.com/seafooler/dumbo/config"
	"github.com/seafooler/dumbo/core"
)

func main() {
	// Create configuration for a single node
	conf := &config.Config{
		Id:           0,
		N:            4,
		F:            1,
		MaxPayloadSize: 5000,
		LogLevel:     1, // INFO level
		MockLatency:  0, // No artificial latency
	}

	// Create node
	node := core.NewNode(conf)
	
	fmt.Printf("Testing Dumbo with 250-byte transactions\n")
	fmt.Printf("Node ID: %d, N: %d, F: %d\n", conf.Id, conf.N, conf.F)
	
	// Test transaction generation
	fmt.Printf("Testing transaction generation...\n")
	
	// Generate some transactions manually
	txs := make([][]byte, 10)
	for i := 0; i < 10; i++ {
		tx := make([]byte, 250)  // 250 bytes per transaction
		tx[249] = byte(i)        // Set identifier in last byte
		txs[i] = tx
	}
	
	// Add transactions to node
	node.AddTransactions(txs)
	
	// Get initial stats
	stats := node.GetStats()
	fmt.Printf("Initial stats: %+v\n", stats)
	
	// Test performance logger
	if node.GetPerfLogger() != nil {
		fmt.Printf("Performance logger initialized successfully\n")
		
		// Get real delays from delay monitor (no artificial simulation)
		delays := map[string]time.Duration{
			"RBC":  0, // Will be updated by real protocol execution
			"ABA":  0,
			"MVBA": 0,
			"ACS":  0,
		}
		
		// Log some test metrics
		// 每个epoch交付的交易量 = batchsize * (N-F) = 5000 * (4-1) = 15000
		deliveredTxsPerEpoch := 5000 * (4 - 1) // batchsize * (N-F)
		deliveredSizePerEpoch := int64(deliveredTxsPerEpoch * 250) // 15000 * 250 = 3750000 bytes
		node.GetPerfLogger().LogEpochMetrics(1, deliveredTxsPerEpoch, deliveredSizePerEpoch, delays)
		node.GetPerfLogger().LogEpochMetrics(2, deliveredTxsPerEpoch, deliveredSizePerEpoch, delays)
		node.GetPerfLogger().LogEpochMetrics(3, deliveredTxsPerEpoch, deliveredSizePerEpoch, delays)
		
		// Get performance metrics
		totalMetrics := node.GetPerfLogger().GetTotalMetrics()
		componentDelays := node.GetPerfLogger().GetComponentDelays()
		
		fmt.Printf("\n=== Performance Metrics ===\n")
		fmt.Printf("Total TPS: %.2f\n", totalMetrics["total_tps"])
		fmt.Printf("Component Delays (ms):\n")
		fmt.Printf("  RBC: %.2f\n", componentDelays["RBC"])
		fmt.Printf("  ABA: %.2f\n", componentDelays["ABA"])
		fmt.Printf("  MVBA: %.2f\n", componentDelays["MVBA"])
		fmt.Printf("  ACS: %.2f\n", componentDelays["ACS"])
	}
	
	// Close node
	node.Close()
	
	fmt.Printf("\nTest completed successfully!\n")
}
